import Vuex from 'vuex'
import Vue from 'vue'
import getters from './getters'
import { UserState } from './modules/user'

export type RootState = {
  user: UserState
}

Vue.use(Vuex)

const modulesFiles = require.context('./modules', true, /\.ts$/)

 const modules: Record<string, any> = modulesFiles.keys().reduce((modules, modulePath) => {
  // set './app.js' => 'app'
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = modulesFiles(modulePath)
  modules[moduleName] = value.default
  return modules
 }, {} as Record<string, any>)


const store = new Vuex.Store<RootState>({
  modules,
  getters
})

export default store
